package com.school.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.school.dao.HljtSubjectManagerDao;
import com.school.entity.HljtSubject;
import com.school.service.HljtSubjectManagerService;
import com.school.util.MapperToJson;
import com.school.util.RepMessage;
import com.school.util.WhetherNotToUpdate;

@Service
public class HljtSubjectManagerServiceImpl implements HljtSubjectManagerService {

	@Autowired
	HljtSubjectManagerDao oHljtSubjectManagerDao;
	
	@Override
	public String findAllSubjectByName(String SubjectName) {
		List<HljtSubject> list=new ArrayList<HljtSubject>();
		int nCount=0;
		RepMessage oRepMessage=new RepMessage();
		oRepMessage.setFlag(true);
		try {
			if(SubjectName!=""&SubjectName!=null){
				list=oHljtSubjectManagerDao.findAllSubjectByName(SubjectName);
				//nCount=oHljtSubjectManagerDao.selectBySubjectName(SubjectName);
			}else{
				list=oHljtSubjectManagerDao.findAllSubjects();
				//nCount=oHljtSubjectManagerDao.selectCountSubject();
			}
		} catch (Exception e) {
			oRepMessage.setFlag(false);
			oRepMessage.setErrInfo("数据异常");
		}
		/*oRepMessage.getObjMap().put("allSubject", list);
		oRepMessage.getObjMap().put("sum", nCount);
		*/
		oRepMessage.setObjData(list);
		return MapperToJson.toJson(oRepMessage);
	}

	@Override
	public String insertSubject(HljtSubject oHljtSubject) {
		int nCount=oHljtSubjectManagerDao.selectBySubjectName(oHljtSubject.getSubjectname());
		RepMessage oRepMessage=new RepMessage();
		if(nCount!=0){
			oRepMessage.setFlag(false);
			oRepMessage.setErrInfo("科目已存在");
		}else{
			oHljtSubjectManagerDao.insertSubject(oHljtSubject);
			oRepMessage.setFlag(true);
		}
		return MapperToJson.toJson(oRepMessage);
	}

	@Override
	public int deleteSubject(int Id) {
		int nCount=oHljtSubjectManagerDao.deleteSubject(Id);
		WhetherNotToUpdate.setPattern(true);
		return nCount;
	}

	@Override
	public String updateSubject(HljtSubject oHljtSubject) {
		int nCount=oHljtSubjectManagerDao.selectBySubjectName(oHljtSubject.getSubjectname());
		RepMessage oRepMessage=new RepMessage();
		if(nCount!=0){
			oRepMessage.setFlag(false);
			oRepMessage.setErrInfo("科目已存在");
		}else{
			oHljtSubjectManagerDao.updateSubject(oHljtSubject);
			oRepMessage.setFlag(true);
		}
		WhetherNotToUpdate.setPattern(true);
		return MapperToJson.toJson(oRepMessage);
	}

}
